// pages/ownerOertification/ownerOertification.js

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    todoid: '',
    detailinfo: {},
    assignorid:'',
    systemid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = wx.getStorageSync("USER_INFO");
    var nickName = wx.getStorageSync("nickName");
    if (options.id) {
      this.data.id = options.id
    }
    if (options.todoid) {
      this.data.todoid = options.todoid
    }
    if (options.todoStatus) {
      this.data.todoStatus = options.todoStatus
    }
    if (options.assignorid) {
      this.data.assignorid = options.assignorid
    }
    if (options.apply) {
      this.data.apply = options.apply
    }
    var id = wx.getStorageSync("id");
    this.data.systemid = id
    this.setData(this.data);
    this.loadData();
    this.readMsg();
  },
  //选择
  changeUsedType: function (e) {
    
    // if (!this.data.areaId) {
    //   util.showModal('请先选择楼栋');
    //   return
    // }
    this.data.areaId = this.data.listareaid[e.detail.value];
    this.data.listareaindex = e.detail.value;
    this.setData(this.data);
    this.loadFloor();

  },
  changefloor: function (e) {
    
    if (!this.data.areaId) {
      util.showModal('请先选择楼层');
      return
    }
    
    this.data.floorId = this.data.listfloorid[e.detail.value];
    this.data.listfloorindex = e.detail.value;
    this.setData(this.data);

  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  changebuild: function (e) {
    
    this.data.buildid = this.data.listbuildid[e.detail.value];
    this.data.listbuildindex = e.detail.value;
    // if (e.detail.value < this.data.areslenth) {
    //   this.setData({
    //     addressshow: false
    //   })
    // }
    this.setData(this.data);
    this.loadAreas();
  },

  //楼栋
  loadData() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/enterpriseStaffs/' + that.data.todoid,
      success(res) {
        

        if (res.data.code == 0) {
          that.data.detailinfo = res.data.data
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  //加载区域
  loadAreas() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/enterpriseStaffs/' + that.data.buildid,
      success(res) {
        
        let listarea = [];
        listarea.push("请选择楼栋")
        let listareaid = [];
        listareaid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listarea.push(item.name)
            listareaid.push(item.id)
          }
          

          that.data.listarea = listarea;
          that.data.listareaid = listareaid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  loadFloor() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/rooms?buildingId=' + that.data.areaId,
      success(res) {
        
        let listfloor = [];
        listfloor.push("请选择房间号")
        let listfloorid = [];
        listfloorid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listfloor.push(item.name)
            listfloorid.push(item.id)
          }
          

          that.data.listfloor = listfloor;
          that.data.listfloorid = listfloorid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: res.data.mesage,
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data.message
        })
      }
    })
  },
  submit() {
    let that = this;
    var id = wx.getStorageSync("id");
    wx.request({
      url: applet_park_api_url + '/api/enterpriseStaffs/' + that.data.todoid,
      method: 'PUT',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        certificationStatus: 3,
        notificationId: that.data.id,
        todoStatus: 4
      },
      success(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: "提交成功",
          })
          var pages = getCurrentPages();

          var prevPage = pages[pages.length - 2]; //上一个页面

          wx.navigateBack();
        } else {
          wx.showToast({
            title: res.data.message
          })
        }
      },
      fail() { }
    })
  },
  refuse() {
    let that = this;
    var id = wx.getStorageSync("id");
    wx.showModal({
      title: '拒绝审核提示',
      content: '您确定要拒绝该员工申请？',
      success: function (res) {
        if (res.confirm) {
          //点击确定按钮
          wx.request({
            url: applet_park_api_url + '/api/enterpriseStaffs/' + that.data.todoid,
            method: 'PUT',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              certificationStatus: 4,
              notificationId: that.data.id,
              todoStatus: 5
            },
            success(res) {
              if (res.data.code == 0) {
                wx.showToast({
                  title: "提交成功",
                })
                var pages = getCurrentPages();

                var prevPage = pages[pages.length - 2]; //上一个页面
               
                wx.navigateBack();
              
              } else {
                wx.showToast({
                  title: res.data.message
                })
              }
            },
            fail() { }
          })
        } else if (res.cancel) {
          //点击取消按钮
        }
      }
    })


  },
  readMsg(){
    let that = this;
    var id = wx.getStorageSync("id");
    if(that.data.apply=='apply'){
      return
    }
    wx.request({
      url: applet_park_api_url + '/api/notifications/' + that.data.id,
      method: 'PUT',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        status: 2,
      },
      success(res) {

      },
      fail() { }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})